<template>
  <div>
    <!-- 上边 -->
    <el-card style="margin-top: 20px 0">
      <!-- 全局组件 -->
      <CategorySelect @getCategoryId="getCategoryId" :show="scene != 0"></CategorySelect>
    </el-card>
    <!-- 下边 -->
    <el-card>
      <!-- 三部分进行切换 -->
      <div v-show="scene == 0">
        <!-- 展示spu列表 -->
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加SPU</el-button>
        <el-table style="width: 100%" border :data="records">
          <el-table-column align="center" type="index" label="序号" width="80"> </el-table-column>
          <el-table-column align="center" prop="spuName" label="Spu名称" width="width"> </el-table-column>
          <el-table-column align="center" prop="description" label="Spu描述" width="width"> </el-table-column>
          <el-table-column align="center" prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <!-- //将来用hintbutton替换 -->
              <el-button type="success" icon="el-icon-plus" size="mini" title="添加Sku" @click="addSku(row)"></el-button>
              <hint-button type="warning" icon="el-icon-edit" size="mini" title="修改Spu" @click="updateSpu(row)"></hint-button>
              <hint-button type="info" icon="el-icon-info" @click="handler(row)" size="mini" title="查看当前Spu全部Sku列表"></hint-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
                <hint-button type="danger" icon="el-icon-delete" size="mini" slot="reference" title="删除Spu"></hint-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- @size-change="handleSizeChange" -->
        <!-- @current-change="handleCurrentChange" -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="getSpuList"
          style="text-align: center"
          :current-page="page"
          :page-size="limit"
          :page-sizes="[3, 5, 10]"
          layout="prev, pager, next,jumper,->,sizes,total"
          :total="total"
        >
        </el-pagination>
      </div>
      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spu"></SpuForm>
      <!-- //0 代表spu列表  1添加修改spu   2 添加sku   -->
      <SkuForm v-show="scene == 2" ref="sku" @changeScenes="changeScenes"></SkuForm>
    </el-card>
    <el-dialog :title="`${spu.spuName}的列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <el-table :data="skuList" border v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="150"></el-table-column>
        <el-table-column prop="price" label="价格" width="200"></el-table-column>
        <el-table-column prop="weight" label="重量"></el-table-column>
        <el-table-column label="默认图片">
          <template slot-scope="{ row, $index }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from './SpuForm'
import SkuForm from './SkuForm'
export default {
  name: 'Spu',
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      // show: true, //控制三级联动的可操作性
      page: 1, //分页器当前第几页
      limit: 3, //每页展示多少数据
      records: [], //spu列表数据
      total: 0, //分页器一共需要展示的数据条数
      scene: 0, //0 代表spu列表  1添加修改spu   2 添加sku
      dialogTableVisible: false, //控制查看弹出 的框
      spu: {},
      skuList: [],
      loading: true, //loading效果
    }
  },
  methods: {
    //三级联动的自定义事件，可以吧相应的id传递给父组件

    getCategoryId({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if (level == 2) {
        this.category2Id = categoryId
        this.category3Id = ''
      } else {
        this.category3Id = categoryId
        //获取SPU列表数据展示
        this.getSpuList()
      }
    },
    //获取SPU列表数据方法
    async getSpuList(pages = 1) {
      this.page = pages
      const { page, limit, category3Id } = this
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id)
      console.log(result) //得到SPU列表数据
      if (result.code == 200) {
        this.records = result.data.records
        this.total = result.data.total
      }
    },
    //点击第几页显示第几页
    // handleCurrentChange(page) {
    //   this.page = page
    //   this.getSpuList()
    // },
    handleSizeChange(limit) {
      this.limit = limit
      this.getSpuList()
    },
    //点击添加页面变化
    addSpu() {
      this.scene = 1 //换页
      //子组件发两个请求
      this.$refs.spu.addSpuData(this.category3Id)
    },
    //修改spu
    updateSpu(row) {
      this.scene = 1
      this.$refs.spu.initSpuData(row)
    },
    changeScene({ scene, flag }) {
      // flag区分保存还是修改
      // console.log(flag)
      this.scene = scene
      if (flag == '修改') {
        this.getSpuList(this.page)
      } else {
        this.getSpuList() //默认page=1
      }
    },
    //删除spu回调
    async deleteSpu(row) {
      console.log(6666)
      let result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code == 200) {
        this.$message({ type: 'success', message: '删除成功' })
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1) //
      }
    },
    //添加Sku
    addSku(row) {
      // 页面切换
      this.scene = 2
      this.$refs.sku.getData(this.category1Id, this.category2Id, row)
    },
    changeScenes(scene) {
      this.scene = scene
    },
    //查看按钮

    async handler(spu) {
      this.dialogTableVisible = true
      this.spu = spu
      //获取
      let result = await this.$API.spu.reqSkuList(spu.id)
      // console.log(result)
      if (result.code == 200) {
        this.skuList = result.data
        this.loading = false
      }
    },
    //关闭弹框触发的事件
    close(done) {
      this.loading = true
      this.skuList = []
      done()
    },
  },
  components: {
    SpuForm,
    SkuForm,
  },
}
</script>

<style></style>
